﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Crawler.Model;
using Husb.Framework.DataAccess;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Crawler.DataAccess
{
    public partial class TaskItemDao : DataAccessBase<TaskItemInfo>
    {
        public TaskItemDao()
        {
            //ObjectId = "Id";
            MasterObjectId = "TaskId";
            //DetailObjectId = "";
            
            CommandType = System.Data.CommandType.StoredProcedure;

            GetByIdStatement = "Crawler.TaskItems_SelectTaskItem";
            //GetByNameStoredProc = SR.SelectWebPageByName;
            FindAllStatement = "Crawler.TaskItems_SelectTaskItemsAll";
            //FindStoredProc = SR.SelectWebPagesDynamic;
            //FindByNameStoredProc = SR.SelectWebPagesByNameInexact;


            InsertStatement = "Crawler.TaskItems_InsertTaskItem";
            UpdateStatement = "Crawler.TaskItems_UpdateTaskItem";
            //CreateUpdateStoredProc = SR.InsertUpdateWebPage;
            DeleteByIdStatement = "Crawler.TaskItems_DeleteTaskItem";
            //DeleteByObjectStoredProc = SR.DeleteWebPage;

            //GetRowCountStoredProc = SR.SelectWebPagesRowCount;
            //GetPagedSortedProc = SR.SelectWebPagesPaged;

            GetByMasterIdStatement = "Crawler.TaskItems_SelectTaskItemsByTaskId";

        }

        #region PopulateTaskItemParamters
        /// <summary>
        /// 根据实体类的属性，给数据库命令的参数赋值
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="cmd">命令对象</param>
        /// <param name="taskItem"></param>
        protected override void PopulateParamters(TaskItemInfo taskItem, Database db, System.Data.Common.DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, taskItem.Id);
            db.AddInParameter(cmd, "TaskId", DbType.Guid, taskItem.TaskId);
            db.AddInParameter(cmd, "Url", DbType.AnsiString, taskItem.Url);
            db.AddInParameter(cmd, "IsNavigation", DbType.Boolean, taskItem.IsNavigation);
            db.AddInParameter(cmd, "AutoPaging", DbType.Boolean, taskItem.AutoPaging);
            db.AddInParameter(cmd, "PageCategory", DbType.Int32, taskItem.PageCategory);
            db.AddInParameter(cmd, "NextPage", DbType.AnsiString, taskItem.NextPage);
        }
        #endregion

        #region  LoadTaskItemInfo
        /// <summary>
        /// 根据数据库的查询结果，给实体类的属性赋值
        /// </summary>
        /// <param name="taskItem"></param>
        /// <param name="dr"><see cref="IDataReader" />类的一个实例</param>
        protected override void LoadProperty(TaskItemInfo taskItem, System.Data.IDataReader dr)
        {
            taskItem.Id = (System.Guid)dr["Id"];
            taskItem.TaskId = (System.Guid)dr["TaskId"];
            taskItem.Url = (System.String)dr["Url"];
            taskItem.IsNavigation = dr["IsNavigation"] == DBNull.Value ? false : (System.Boolean?)dr["IsNavigation"];
            taskItem.AutoPaging = dr["AutoPaging"] == DBNull.Value ? false : (System.Boolean?)dr["AutoPaging"];
            taskItem.PageCategory = dr["PageCategory"] == DBNull.Value ? (int)0 : (System.Int32?)dr["PageCategory"];
            taskItem.NextPage = dr["NextPage"] == DBNull.Value ? "" : (System.String)dr["NextPage"];
        }
        #endregion

        #region LoadHalfTaskItemInfo
        /// <summary>
        /// 根据执行不同的数据获取命令，得到一个不完整的TaskItemInfo<see cref="TaskItemInfo" />对象，也许只有部分属性有内容。
        /// </summary>
        /// <param name="taskItem"></param>
        /// <param name="dr">IDataReader<see cref="IDataReader" /></param>
        public static void LoadHalfTaskItemInfo(TaskItemInfo taskItem, System.Data.IDataReader dr)
        {
            for (int fieldCount = 0; fieldCount < dr.FieldCount; fieldCount++)
            {
                switch (dr.GetName(fieldCount))
                {
                    case "Id":
                        taskItem.Id = (System.Guid)dr["Id"];
                        break;
                    case "Url":
                        taskItem.Url = (System.String)dr["Url"];
                        break;
                    case "IsNavigation":
                        taskItem.IsNavigation = dr["IsNavigation"] == DBNull.Value ? false : (System.Boolean?)dr["IsNavigation"];
                        break;
                    case "AutoPaging":
                        taskItem.AutoPaging = dr["AutoPaging"] == DBNull.Value ? false : (System.Boolean?)dr["AutoPaging"];
                        break;
                    case "PageCategory":
                        taskItem.PageCategory = dr["PageCategory"] == DBNull.Value ? (int)0 : (System.Int32?)dr["PageCategory"];
                        break;
                    case "NextPage":
                        taskItem.NextPage = dr["NextPage"] == DBNull.Value ? "" : (System.String)dr["NextPage"];
                        break;
                }
            }
        }
        #endregion
    }
}
